package com.wyl.clinic.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyl.clinic.common.QueryPageParam;
import com.wyl.clinic.common.Result;
import com.wyl.clinic.entity.Department;
import com.wyl.clinic.service.DepartmentService;
import com.wyl.clinic.service.DoctorService;
import com.wyl.clinic.service.DoctorWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 科室表 前端控制器
 * </p>
 *
 * @author wangyulong
 * @since 2024-02-20
 */
@RestController
    @RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");

        Page<Department> page = new Page(1,2);
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Department::getName,name);
        }


        IPage result = departmentService.page(page,lambdaQueryWrapper);

        return Result.suc(result.getRecords(),result.getTotal());
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Department department){
        return departmentService.saveOrUpdate(department)?Result.suc():Result.fail();
    }

    @GetMapping("/list")
    public Result list(){
        return Result.suc(departmentService.list());
    }

    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        return departmentService.removeById(id)?Result.suc():Result.fail();
    }

}
